package com.istock.union.user.runner;

import java.io.File;

import org.apache.commons.dbcp.BasicDataSource;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserRunner {

	private static Logger logger = LoggerFactory.getLogger(UserRunner.class);
	public static void main(String[] args) throws Exception {
		logger.info("==============================");
		
		QueuedThreadPool boundedThreadPool = new QueuedThreadPool();
		boundedThreadPool.setMaxThreads(8);
		
		Server server = new Server(boundedThreadPool);
		
		HttpConfiguration http_config = new HttpConfiguration();
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);
		
		ServerConnector http = new ServerConnector(server,new HttpConnectionFactory(http_config));
		http.setPort(18081);
//		http.setIdleTimeout(30000);
		server.addConnector(http);
		
        /*File keystoreFile = new File("src/test/resources/jetty/keystore");
        System.out.println(keystoreFile.getAbsolutePath()+"=========="+keystoreFile.exists());
		SslContextFactory sslContextFactory = new SslContextFactory();
	    sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
	    sslContextFactory.setKeyStorePassword("123456");
	    sslContextFactory.setKeyManagerPassword("123456");
        ServerConnector https = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),new HttpConnectionFactory(http_config));
        https.setPort(8443);
		server.addConnector(https);*/
        
		
		org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
        classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
		
        WebAppContext webapp = new WebAppContext("src/main/webapp", "/new-user");
//        webapp.setDefaultsDescriptor(new File("src/test/resources/jetty/jetty-descriptor.xml").toURI().getPath());
        
        
        
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://senvon.vm:3306/new-user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull");
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        dataSource.setMaxWait(2000);
        dataSource.setMaxActive(2);
        dataSource.setMaxIdle(2);
        org.eclipse.jetty.plus.jndi.Resource mydatasource = new org.eclipse.jetty.plus.jndi.Resource(webapp, "jdbc/dataSource",dataSource);
        
        /*EnvConfiguration envConfiguration = new EnvConfiguration();
        envConfiguration.setJettyEnvXml(new File("src/test/resources/jetty/jetty-env.xml").toURI().toURL());
        Configuration[] configurations = new Configuration[]{
		        new WebInfConfiguration(),
        		new WebXmlConfiguration(),
		        envConfiguration,
		        new PlusConfiguration(),
		        new JettyWebXmlConfiguration()
		};
        webapp.setConfigurations(configurations);*/
		
        
        server.setHandler(webapp);
        
        server.start();
		System.out.println("============================the server is started===========================");
		server.join();
	}
}